package com.cj.framework.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.cj.framework.app.BaseApplication;


/**
 * Created by cj on 2016/6/4.
 * Email:781806481@qq.com
 */
public class SP {
    private static final String FILE_NAME="framework";
    private static SharedPreferences sharedPreferences= BaseApplication.getContext().getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
    private static SharedPreferences.Editor editor=sharedPreferences.edit();
    /**
     * 添加一个键值对或者修改该键对应的值
     * @param name
     * @param object
     */
    public static void add(String name,Object object){

        switch (object.getClass().getSimpleName()){
            case "Integer":
                editor.putInt(name,(Integer)object);
                break;
            case "Long":
                editor.putLong(name,(Long)object);
                break;
            case "Float":
                editor.putFloat(name, (Float) object);
                break;
            case "Double":
                editor.putFloat(name,(float)(((Double)object).doubleValue()));
                break;
            case "Boolean":
                editor.putBoolean(name, (Boolean) object);
                break;
            case "String":
                editor.putString(name, (String) object);
                break;
        }
        editor.commit();
    }

    /**
     * 删除一个键值对当无此键的时候不报错
     * @param name
     */
    public static void delete(String name){
        editor.remove(name);
        editor.commit();
    }

    /**
     * 获取String值。当无此键值对的时候返回null，其余get方法一样
     * @param name
     * @return
     */
    public static String getString(String name){
       return sharedPreferences.getString(name,null);
    }

    /**
     * 获取boolean值
     * @param name
     * @return
     */
    public static boolean getBoolean(String name){
        return sharedPreferences.getBoolean(name,false);
    }

    /**
     * 获取int值
     * @param key
     * @return
     */
    public static int getInt(String key){
        return sharedPreferences.getInt(key,0);
    }

    /**
     * 获取long值
     * @param key
     * @return
     */
    public static long getLong(String key){
        return sharedPreferences.getLong(key,0l);
    }

    /**
     * 获取float值
     * @param key
     * @return
     */
    public static float getFloat(String key){
        return sharedPreferences.getFloat(key,0f);
    }

}
